package com.cyj.crm02.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.crm02.entity.Employee;
import com.cyj.crm02.service.EmployeeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping(value="/login",produces = "application/json;charset=UTF-8")
    public Map login(@RequestBody Map<String, String> map01){
        Map<String,Object> result = new HashMap<String, Object>();
        System.out.println("employeeName:"+map01.get("employeeName"));
        System.out.println("password:"+map01.get("password"));

        //拿到当前主体对象，用作验证   （从安全工具类中拿到subject）
        Subject subject = SecurityUtils.getSubject();
        //将用户名和密码包装到token中
        UsernamePasswordToken token = new UsernamePasswordToken(map01.get("employeeName"),map01.get("password"));
        //通过捕获异常来判断用户是否是否可以登录
        try{
            subject.login(token);
            result.put("code",200);
            result.put("msg","登录成功");
        }catch (UnknownAccountException uae){
            result.put("code",100);
            result.put("msg","用户名不存在");
            System.out.println("用户名不存在");
        }catch (IncorrectCredentialsException ice){
            result.put("code",100);
            result.put("msg","密码错误");
            System.out.println("密码错误");
        }

        return result;
    }

    /**
     *  查询一页员工信息
     *
     */
    @GetMapping("/list")
    public Map getList(Integer page,Integer limit ){
        Map<String,Object> map = new HashMap<String,Object> ();
        Page<Employee> page1 = new Page<Employee>();
        page1.setSize(limit);
        page1.setCurrent(page);

        IPage<Employee> iPage = employeeService.selectEmployeeList(page1);

        map.put("msg","查询情况");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);

        System.out.println(map);
        return map;
    }

    /**
     *  新增员工
     */
    @PostMapping(value="/add",produces = "application/json;charset=UTF-8")
    public Map add(Employee employee,Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        int num = employeeService.addEmployee(employee,roleId);
        result.put("state",num);
        if(num ==1){
            result.put("msg","添加成功");
        }else {
            result.put("msg","添加失败");
        }
        return result;
    }

    /**
     *  删除员工
     */
    @DeleteMapping("/del/{employeeId}")
    public Map del(@PathVariable Integer employeeId){
        Employee employee = new Employee();
        employee.setEmployeeId(employeeId);
        employee.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        int num = 0;
        boolean flag = employeeService.updateById(employee);
        if (flag==true){
            num = 1;
            result.put("msg","删除成功");
        }else {
            result.put("msg","删除失败");
        }
        result.put("state",num);
        return result;
    }

    /**
     *  修改员工
     */
    @PutMapping("/update")
    public Map edit(Employee employee,Integer roleId){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",employeeService.update(employee,roleId));
        return result;
    }


}
